package com.leoandroid.tool.toolsbox.largeread.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.LinearLayout;

import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.leoandroid.tool.toolsbox.largeread.utils.FlashlightUtils;
import com.leoandroid.tool.toolsbox.largeread.R;

public class FlashLightActivity extends BaseActivity{
    private static final String TAG = "FlashLightActivity";
    private LinearLayout layoutClose;
    private RangeSeekBar flashProgressBar;

    private int curTimeInterval;

    private static final int MSG_FLASH_LIGHT_ON = 1;
    private static final int MSG_FLASH_LIGHT_OFF = 2;


    private static final int DELAY_TIME = 500;


    @SuppressLint("HandlerLeak")
    private Handler mhandler = new  Handler(){
        // 通过复写handlerMessage()从而确定更新UI的操作
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_FLASH_LIGHT_ON:
                    FlashlightUtils.linghtOn();
                    mhandler.sendEmptyMessageDelayed(MSG_FLASH_LIGHT_OFF, curTimeInterval);
                    break;
                case MSG_FLASH_LIGHT_OFF:
                    FlashlightUtils.linghtOff();
                    mhandler.sendEmptyMessageDelayed(MSG_FLASH_LIGHT_ON, curTimeInterval);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash_light);
        curTimeInterval = DELAY_TIME;
        initView();
        mhandler.sendEmptyMessageDelayed(MSG_FLASH_LIGHT_ON, DELAY_TIME);
    }

    private void initView(){
        layoutClose = findViewById(R.id.layout_close);
        flashProgressBar = findViewById(R.id.flash_progress);

        layoutClose.setOnClickListener(view -> {
            finish();
        });
        flashProgressBar.setRange(100f, 1000f, 100f);
        flashProgressBar.setProgress(500);
        flashProgressBar.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                Log.d(TAG, "leftValue = " + leftValue + ", rightValue =  " + rightValue + ", isFromUser = " + isFromUser);
                curTimeInterval = (int) leftValue;
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {
                Log.d(TAG, "view.getProgressRadius() = " + view.getProgressRadius());
                mhandler.removeMessages(MSG_FLASH_LIGHT_ON);
                mhandler.removeMessages(MSG_FLASH_LIGHT_OFF);
                mhandler.sendEmptyMessageDelayed(MSG_FLASH_LIGHT_ON, curTimeInterval);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        mhandler.removeMessages(MSG_FLASH_LIGHT_ON);
        mhandler.removeMessages(MSG_FLASH_LIGHT_OFF);
        FlashlightUtils.linghtOff();
    }
}
